package com.cyou.qb.constants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.context.ApplicationContext;

public final class Constants implements ConstantsMBean{
	
	public static ApplicationContext ctx;
	//默认密码
	public static String ADMIN_USER_DEFAULT_PWD="913a7817a1a9d45ca203ac1c6a5263a5";

//	public static int SERVICE_OF_173_TIMEOUT= 10000;

	public static final int MAX_LOGINFO_BYTE_LENGTH = 2000;//日志最大长度
	
	//访问图片服务器
	public static String FILE_SERVER_HOST="";
	public static String FILE_SERVER_PORT="";
	public static String FILE_SERVER_URI="";
	public static String UPLOAD_FILE_SIZE_MAX="";

	//是否需要高频访问控制
	public static boolean FREQUENCY_INTERCEPTOR=false;
	
	//是高频访问控制时间片 单位秒
	public static int FREQUENCY_TIME_SEGMENT_SECOND=10;
	
	//是高频访问控制时间片内的最大次数
	public static int FREQUENCY_TIMES=100;
	
	public static int TOP_WINNER_SIZE = 20;
	public static String defaultAward1Image;
	public static String defaultAward1Name;

	
	public static String defaultAward2Image;
	public static String defaultAward2Name;

	
	public static String defaultAward3Image;
	public static String defaultAward3Name;

	
	public static String defaultAward4Image;
	public static String defaultAward4Name;

	
	
	public static Properties pt = null;

    static {
        pt = new Properties();
        InputStream ist = null;
        try {
            ist = Constants.class.getClassLoader().getResourceAsStream("dynamic-constants.properties");
            pt.load(ist);
            try {
            	ADMIN_USER_DEFAULT_PWD = pt.getProperty("admin_user_default_pwd");
//            	SERVICE_OF_173_TIMEOUT =Integer.parseInt(pt.getProperty("service_of_173_timeout"));
            	
            	defaultAward1Image = pt.getProperty("defaultAward1Image");
             	defaultAward1Name = pt.getProperty("defaultAward1Name");
            	defaultAward2Image = pt.getProperty("defaultAward2Image");
            	defaultAward2Name = pt.getProperty("defaultAward2Name");
            	defaultAward3Image = pt.getProperty("defaultAward3Image");
            	defaultAward3Name = pt.getProperty("defaultAward3Name");
            	defaultAward4Image = pt.getProperty("defaultAward4Image");
            	defaultAward4Name = pt.getProperty("defaultAward4Name");
            	
            	FILE_SERVER_HOST = pt.getProperty("file_server_host");
            	FILE_SERVER_PORT = pt.getProperty("file_server_port");
            	FILE_SERVER_URI = pt.getProperty("file_server_uri");
            	UPLOAD_FILE_SIZE_MAX= pt.getProperty("upload_file_size_max");
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
            	if(null != ist) {
            		ist.close();
            	}
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void updateProperties() {
        FileOutputStream fost = null;
        try {
            fost = new FileOutputStream(new File(Constants.class.getClassLoader().getResource("dynamic-constants.properties").getPath()));
            pt.store(fost, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fost.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
//
//	@Override
//	public int getServiceOf173TimeOut() {
//		return SERVICE_OF_173_TIMEOUT;
//	}
//
//	@Override
//	public void setServiceOf173TimeOut(int serviceOf173TimeOut) {
//		SERVICE_OF_173_TIMEOUT=serviceOf173TimeOut;
//		pt.put("service_of_173_timeout", serviceOf173TimeOut+"");
//		updateProperties();
//	}

	@Override
	public String getAdminUserDefaultPwd() {
		return ADMIN_USER_DEFAULT_PWD;
	}

	@Override
	public void setAdminUserDefaultPwd(String adminUserDefaultPwd) {
		ADMIN_USER_DEFAULT_PWD=adminUserDefaultPwd;
		pt.put("admin_user_default_pwd", adminUserDefaultPwd);
		updateProperties();
	}
}
